package com.cbtx.module_sp.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.cbtx.module.base.http.HttpManager
import com.cbtx.module.base.http.MyRequestCallBack
import com.cbtx.module.base.vm.BaseVM
import com.cbtx.module.common.bean.GoodsEntity
import com.cbtx.module.common.http.HttpUrlManager
import com.cbtx.module.common.utils.DataCacheUtil
import com.cbtx.module_sp.MyApplication
import com.cbtx.module_sp.interceptor.HeaderInterceptor

/**
 * @author : Lambert
 * date   : 2020/10/26 9:03 PM
 */
class GoodsTreasureFragmentVM(application: Application) : BaseVM(application) {

    val loadFirst = -1
    val loading = 0
    val loadEnd = 1;
    val loadError = 2;

    var mCanLoadMore = false
    var mPageNo = 1
    val mPageSize = 50


    val mDataLoadStatus: MutableLiveData<Int> = MutableLiveData();
    val mDatas: MutableList<GoodsEntity.DataBean.ListBean> = ArrayList()
    var reConnectCount = 0

    fun init() {
        //先拿缓存数据
        val goodsTreasureList: MutableList<GoodsEntity.DataBean.ListBean> =
            DataCacheUtil.readGoodsTreasureList()
        if (goodsTreasureList != null) {
            mDatas.addAll(goodsTreasureList)
        }

    }

    fun requestData(isRefresh: Boolean) {

        mCanLoadMore = false

        if (isRefresh) {
            mPageNo = 1;
        } else {
            mPageNo++
        }

        val params: MutableMap<String, Any> = HashMap()
        params["pageNum"] = mPageNo
        params["pageSize"] = mPageSize
        params["type"] = "3" //藏宝阁数据
        HttpManager.getInstance("",
            HeaderInterceptor(MyApplication.instance.getHeaderMap())
        )
            .sendGet(
                HttpUrlManager.TREASURE_GOODS_LIST,
                params,
                object : MyRequestCallBack<GoodsEntity>() {
                    override fun onSuccess(obj: GoodsEntity?) {

                        resetData(obj, isRefresh)
                    }

                    override fun onFailure(error: Int, msg: String?) {
                        //如果是刷新失败，则再次刷新
                        if (isRefresh && reConnectCount < 1) {
                            reConnectCount++
                            requestData(true)
                        } else {
                            mDataLoadStatus.value = loadError
                        }
                    }

                })
    }

    fun resetData(obj: GoodsEntity?, isRefresh: Boolean) {
        val itemList: MutableList<GoodsEntity.DataBean.ListBean> = ArrayList()
        if (obj?.data != null) {
            itemList.addAll(obj.data.list)
        }


//        for(renZhengItem in itemList){
//            renZhengItem.image = imgUrlBase+renZhengItem.image;//+"?x-oss-process=image/resize,m_mfit,w_280,crop,h_280,w_280,h_280,g_center"
//        }


        if (isRefresh) {
//            mGoodsTreasureList.addAll(mDatas?.value!!)
            mDatas.clear()
        }


        mDatas.addAll(itemList)

        if (itemList.size >= mPageSize) {
            mCanLoadMore = true
        }

//        SharedPreferencesUtils.saveObject(mApplication)

        DataCacheUtil.saveGoodsTreasureList(mDatas)

        mDataLoadStatus.value = loadEnd
    }

}